package com.pkk.log.web.handler;

import com.alibaba.fastjson.JSONObject;
import com.pkk.log.entity.log.SysLogEntity;
import com.pkk.log.service.SysLogService;
import com.pkk.support.penguin.business.log.configuration.BusinessLogQueueConfiguration;
import java.util.Optional;
import javax.annotation.Resource;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @description: 日志消息接收
 * @author: peikunkun
 * @create: 2019-06-10 16:19
 **/
@Component
public class SysLogRabbitMqReceive {

  @Resource
  private SysLogService sysLogService;

  /**
   * @Description: 日志消息处理
   * @Param: [sysLog]
   * @return: void
   * @Author: peikunkun
   * @Date: 2019/6/10 0010 下午 5:08
   */
//  @MonitorTransaction
  @RabbitListener(queues = BusinessLogQueueConfiguration.BUSINESS_LOG_QUEUE)
  public void handleSysLogMq(String sysLog) {
    try {
      SysLogEntity sysLogEntityEntity = JSONObject.parseObject(sysLog, SysLogEntity.class);
      sysLogEntityEntity.setUsername(Optional.ofNullable(sysLogEntityEntity.getUsername()).orElseGet(()->"未知操作人"));
      sysLogEntityEntity.setDepict(Optional.ofNullable(sysLogEntityEntity.getDepict()).orElseGet(()->"未知操作描述"));
      final boolean b = sysLogService.save(sysLogEntityEntity);
      if (!b) {
        throw new RuntimeException("处理日志失败");
      }
    } catch (Exception e) {
      throw new RuntimeException("处理日志异常");
    }
  }


}
